import time

from sam.util.fileUtil2 import read_file_quick, write_file_quick
from sam.util.httpUtil2 import BaseBrowser
from sam.util.notifyUtil import work_we_chart_notify
from sam.util.strUtil import extract_number, filter_symbol_4_word

RICH_TEXT_ELEMENT_LIST = ["p", "ul", "blockquote", "h2"]


class ZhiHuQuestion(BaseBrowser):
    def __init__(self):
        super().__init__()

    def login(self):
        self.browser.maximize_window()
        self.browser.get("https://www.zhihu.com/signin")
        self.click("div.SignFlow-qrcodeTab")
        time.sleep(20)

    def extract_answer_list_count(self):
        # 先判断是否有展开全部答案
        extend_all_answer_list = self.browser.find_elements_by_css_selector(
            "div.Question-mainColumn  div.ViewAll  a.QuestionMainAction")
        if extend_all_answer_list:
            self.browser.execute_script("arguments[0].click();", extend_all_answer_list[0])
            time.sleep(5)
        all_answer_list_str = self.browser.find_element_by_css_selector("h4.List-headerText").text.strip()
        return extract_number(all_answer_list_str)

    def get_answer_item_content_str(self, item_element):
        under_item_element_list = item_element.find_elements_by_css_selector("span.CopyrightRichText-richText > *")
        if under_item_element_list:
            under_item_element_str_list = []
            for under_item_element in under_item_element_list:
                tag_name = under_item_element.tag_name
                class_name = under_item_element.get_attribute("class")
                text = under_item_element.text
                if tag_name in RICH_TEXT_ELEMENT_LIST:
                    if class_name == 'ztext-empty-paragraph':
                        continue
                    else:
                        under_item_element_str_list.append(text.strip())
                else:
                    continue
            else:
                return " ".join(under_item_element_str_list)
        else:
            return item_element.text.strip()

    def get_question_all_answer(self, url):
        answer_list = []
        self.browser.get(url)
        total_count = self.extract_answer_list_count()
        h4_element = self.browser.find_element_by_css_selector("h4.List-headerText")
        self.click_on(h4_element)
        if total_count:
            count_item_list = self.browser.find_elements_by_css_selector("div.ContentItem-time")
            while len(count_item_list) < total_count:
                last_item_element = count_item_list[-1]
                self.move_to2(last_item_element)
                count_item_list = self.browser.find_elements_by_css_selector("div.ContentItem-time")
                collapse_selector_list = self.browser.find_elements_by_css_selector("div.CollapsedAnswers-bar")
                if collapse_selector_list:
                    break
                else:
                    pass

            item_element_list = self.browser.find_elements_by_css_selector("span.CopyrightRichText-richText")
            for item_element in item_element_list:
                item_content_str = self.get_answer_item_content_str(item_element)
                if item_content_str:
                    word = filter_symbol_4_word(
                        s=item_content_str
                        , is_need_formatter=True
                        , is_need_filter_start_end=True
                    )
                    answer_list.append(word)

        return answer_list

    def main(self):
        self.login()
        line_list = read_file_quick(file_type="txt")
        for line in line_list:
            split = line.split("$")
            title = split[0]
            url = split[1]
            print(f" 将要抓取 {title}")
            text_list = self.get_question_all_answer(url)
            if text_list:
                write_file_quick(data_list=text_list, file_type="txt", export_file_name=title)
                work_we_chart_notify(f" 完成了 {title} ")
            else:
                work_we_chart_notify(f" {title} 爬取失败")

        print("抓取完了")
        work_we_chart_notify(f" 全部 抓取完了 ")


def zhi_hu_question():
    process = ZhiHuQuestion()
    process.main()


def test():
    text_list = [
        "抓取完了"
        , "抓取完了2"
    ]
    write_file_quick(data_list=text_list, file_type="txt", export_file_name="测试")


if __name__ == "__main__":
    zhi_hu_question()
    # test()
